package com.ucenter.server.auth.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.system.comm.enums.Boolean;
import com.system.comm.model.Page;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;
import com.ucenter.server.auth.dao.RoleResDao;
import com.ucenter.server.auth.pojo.ResInfo;
import com.ucenter.server.auth.pojo.RoleInfo;
import com.ucenter.server.auth.pojo.RoleRes;
import com.ucenter.server.auth.service.ResInfoService;
import com.ucenter.server.auth.service.RoleInfoService;
import com.ucenter.server.auth.service.RoleResService;

/**
 * role_res的Service
 * @author autoCode
 * @date 2018-12-01 09:50:44
 * @version V1.0.0
 */
@Component
public class RoleResServiceImpl implements RoleResService {

	@Autowired
	private RoleResDao roleResDao;
	@Autowired
	private ResInfoService resInfoService;
	@Autowired
	private RoleInfoService roleInfoService;
	
	@Override
	public ResponseFrame save(RoleRes roleRes) {
		ResponseFrame frame = new ResponseFrame();
		RoleRes org = get(roleRes.getRoleId(), roleRes.getResId());
		if(org == null) {
			roleResDao.save(roleRes);
		}
		frame.setSucc();
		return frame;
	}

	@Override
	public RoleRes get(String roleId, String resId) {
		return roleResDao.get(roleId, resId);
	}

	@Override
	public ResponseFrame pageQuery(RoleRes roleRes) {
		roleRes.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = roleResDao.findRoleResCount(roleRes);
		List<RoleRes> data = null;
		if(total > 0) {
			data = roleResDao.findRoleRes(roleRes);
		}
		Page<RoleRes> page = new Page<RoleRes>(roleRes.getPage(), roleRes.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String roleId, String resId) {
		ResponseFrame frame = new ResponseFrame();
		roleResDao.delete(roleId, resId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<ResInfo> findResAllByRoleId(String roleId) {
		RoleInfo role = roleInfoService.get(roleId);
		List<ResInfo> list = resInfoService.findByWebsiteId(role.getWebsiteId());
		List<RoleRes> selList = roleResDao.findByRoleId(roleId);
		for (ResInfo res : list) {
			res.setIsAuth(Boolean.FALSE.getCode());
			for (RoleRes rr : selList) {
				if (res.getResId().equals(rr.getResId())) {
					res.setIsAuth(Boolean.TRUE.getCode());
					break;
				}
			}
		}
		return list;
	}
}